﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.IO;
using System.Text;

/// <summary>
/// Summary description for reuseCode
/// </summary>
public class reuseCode
{
    VLGDataContext _dbvip = new VLGDataContext();
	public reuseCode()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static string CompressCSS(string body)
    {
        body = Regex.Replace(body, "/\\*.+?\\*/", "", RegexOptions.Singleline);
        body = body.Replace("  ", string.Empty);
        body = body.Replace(Environment.NewLine + Environment.NewLine + Environment.NewLine, string.Empty);
        body = body.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
        body = body.Replace(Environment.NewLine, string.Empty);
        body = body.Replace("\\t", string.Empty);
        body = body.Replace(" {", "{");
        body = body.Replace(" :", ":");
        body = body.Replace(": ", ":");
        body = body.Replace(", ", ",");
        body = body.Replace("; ", ";");
        body = body.Replace(";}", "}");
        body = body.Replace("\\n ", "");
        body = body.Replace("\\b", "");
        body = Regex.Replace(body, "/\\*[^\\*]*\\*+([^/\\*]*\\*+)*/", "$1");
        body = Regex.Replace(body, "(?<=[>])\\s{2,}(?=[<])|(?<=[>])\\s{2,}(?=&nbsp;)|(?<=&ndsp;)\\s{2,}(?=[<])", string.Empty);

        return body;
    }
    // Lay tat ca file Css
    public static IList<System.IO.FileInfo> GetFiles(string serverPath, string extention)
    {
        if (!serverPath.StartsWith("~/"))
        {
            if (serverPath.StartsWith("/"))
                serverPath = "~" + serverPath;
            else
                serverPath = "~/" + serverPath;
        }

        string path = HttpContext.Current.Server.MapPath(serverPath);

        if (!path.EndsWith("/"))
            path = path + "/";

        if (!Directory.Exists(path))
            throw new System.IO.DirectoryNotFoundException();

        IList<FileInfo> files = new List<FileInfo>();

        string[] fileNames = Directory.GetFiles(path, "*." + extention, System.IO.SearchOption.AllDirectories);
        foreach (string name in fileNames)
            files.Add(new FileInfo(name));

        return files;
    }
    // Combine lai
    public static string CombineCSS()
    {
        string allCSS = string.Empty;

        foreach (FileInfo fi in GetFiles("css/", "css"))
        {
            using (StreamReader sr = new StreamReader(fi.FullName))
                allCSS += sr.ReadToEnd();
        }

        allCSS = allCSS.Replace("~/", ConfigurationManager.AppSettings["global"].ToString());

        allCSS = CompressCSS(allCSS);

        return allCSS;
    }
    // Combine lai
    public static string CombineJs()
    {
        string allCSS2 = string.Empty;

        foreach (FileInfo fi in GetFiles("js/", "js"))
        {
            using (StreamReader sr = new StreamReader(fi.FullName))
                allCSS2 += sr.ReadToEnd();
        }

        allCSS2 = allCSS2.Replace("~/", ConfigurationManager.AppSettings["global"].ToString());

        allCSS2 = CompressCSS(allCSS2);

        return allCSS2;
    }
    
}
